package com.hckjsec.cloud.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.AppletsPublicNews;
import com.hckjsec.cloud.service.IAppletsPublicNewsService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 新闻动态Controller
 * 
 * @author cyt
 * @date 2021-09-04
 */
@RestController
@RequestMapping("/system/news")
public class AppletsPublicNewsController extends BaseController
{
    @Autowired
    private IAppletsPublicNewsService appletsPublicNewsService;

    /**
     * 查询新闻动态列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppletsPublicNews appletsPublicNews)
    {
        startPage();
        appletsPublicNews.setDeptId(SecurityUtils.getDeptId());
        List<AppletsPublicNews> list = appletsPublicNewsService.selectAppletsPublicNewsList(appletsPublicNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻动态列表
     */
    @Log(title = "新闻动态", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletsPublicNews appletsPublicNews)
    {
        appletsPublicNews.setDeptId(SecurityUtils.getDeptId());
        List<AppletsPublicNews> list = appletsPublicNewsService.selectAppletsPublicNewsList(appletsPublicNews);
        ExcelUtil<AppletsPublicNews> util = new ExcelUtil<AppletsPublicNews>(AppletsPublicNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取新闻动态详细信息
     */
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return AjaxResult.success(appletsPublicNewsService.selectAppletsPublicNewsById(newsId));
    }

    /**
     * 新增新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletsPublicNews appletsPublicNews)
    {
        appletsPublicNews.setDeptId(SecurityUtils.getDeptId());
        appletsPublicNews.setOperator(SecurityUtils.getUserId());
        if (!"".equals(StringUtils.isHcNull(appletsPublicNews.getContent(),""))){//解密
            appletsPublicNews.setContent(Base64Converter.decode(appletsPublicNews.getContent()));
        }else{
            appletsPublicNews.setContent(appletsPublicNews.getContent());
        }
        return toAjax(appletsPublicNewsService.insertAppletsPublicNews(appletsPublicNews));
    }

    /**
     * 修改新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletsPublicNews appletsPublicNews)
    {
        if (!"".equals(StringUtils.isHcNull(appletsPublicNews.getContent(),""))){//解密
            appletsPublicNews.setContent(Base64Converter.decode(appletsPublicNews.getContent()));
        }else{
            appletsPublicNews.setContent(appletsPublicNews.getContent());
        }
        return toAjax(appletsPublicNewsService.updateAppletsPublicNews(appletsPublicNews));
    }

    /**
     * 删除新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(appletsPublicNewsService.deleteAppletsPublicNewsByIds(newsIds));
    }
}
